#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<functional>
using namespace std;


class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder() {

    }
    
    void addNum(int num) {
        if(left.empty()&&right.empty()){
            left.push(num);
        }
        else if(left.size()==right.size()){
            //num要放进来
            if(num<=right.top()){
                left.push(num);
            }
            else{
                left.push(right.top());
                right.pop();
                right.push(num);
            }
        }
        else if(left.size()>right.size()){
            if(num>=left.top()){
                right.push(num);
            }
            else{
                right.push(left.top());
                left.pop();
                left.push(num);
            }
        }
    }
    
    double findMedian() {
        if((left.size()+right.size())%2==0){
            return (left.top()+right.top())/2.0;
        }
        else{
            return left.top();
        }

    }
public:
    priority_queue<int> left;
    priority_queue<int,vector<int>,greater<int>> right;
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */

int main(){
    MedianFinder M;
    M.addNum(1);
    M.addNum(2);
    cout<<M.findMedian()<<endl;
    M.addNum(3);
    cout<<M.findMedian()<<endl;
}